
#### APPENDIX B: summary statistics

# load, combine data
library(rio)
library(tidyverse)

# generate tables
library(stargazer)

# generate correlation matrix
library(corrplot)




# LOAD DATA
glm_df <- import("glm_data.csv") %>%
  filter(iso3c!="TTO" & iso3c!="GUY" & iso3c!="SUR") %>%
  filter(!is.na(approval) & !is.na(opp1vote) & !is.na(number_protests))



summary_statistics <- glm_df %>%
  select(year,doc,fund_doc,approval,opp1vote,number_protests,duck,partyage,polity2,left_exec,any_election,
         minister_is_technocrat_mainstream,resource_rents_lag,discovery,real_oil_price_log,gdp_pc_constant_lag,
         gdp_growth_lag,imf_program,crisis) 

summary_statistics <- as.data.frame(summary_statistics)

stargazer(summary_statistics, title="Summary Statistics",
          digit.separate=0, #type="text",
          omit.summary.stat=c("p25","p75"),
          covariate.labels = c("Year","Any document","Fund document",
                               "Executive approval (perc)",
                               "Opposition vote share (perc)","Number of protests","Lame duck","Party age",
                               "Democracy","Left executive","Election quarter","Mainstream minister",
                               "Resource rents (perc)","Field discovery","Oil price (USD, log)",
                               "GDP per capita (1,000 USD)","GDP growth (perc)","IMF program", "Crisis"))


## correlation between key independent variables
corr_matrix <- glm_df %>%
  select(approval,opp1vote,number_protests) %>%
  drop_na() %>%
  rename(`Number of protests` = number_protests,
         `Executive approval` = approval,
         `Opposition vote share` = opp1vote)

cor(corr_matrix)
col <- colorRampPalette(c("#440154FF", "white", "#21908CFF"))(20)


# pdf(file = "correlation_matrix.pdf", height = 7)
corrplot(cor(corr_matrix), tl.col="black", col = col, type = "upper")
# dev.off()

